package com.jc.service.task.proTypeAttribute;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.*;
import com.jc.ds.model.*;
import com.jc.framework.enums.ThirdPartyParam;
import com.jc.service.attribute.AttributeService;
import com.jc.service.attributevalue.AttributeValueService;
import com.jc.service.h5.ShopService;
import com.jc.service.producttype.ProductTypeService;
import com.jc.service.producttypeattribute.ProductTypeAttributeService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

@SuppressWarnings("all")
@Service
public class ProTypeAttributeServiceImpl implements ProTypeAttributeService {
    private Long ts = new Date().getTime();
    public Logger getLoger() {
        return LoggerFactory.getLogger(this.getClass());
    }

    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ProductTypeAttributeService productTypeAttributeService;
    @Autowired
    private AttributeService attributeService;
    @Autowired
    private AttributeValueService attributeValueService;
    @Autowired
    private ShopService shopService;

    @Transactional
    @Override
    public Map<String,Object> synProTypeAttribute() throws IOException, URISyntaxException {
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Object> param = new HashMap<>();
        param.put("code",shopService.getShopConfig().getCODE());
        param.put("encryptedString", MD5.passmd5("code="+shopService.getShopConfig().getCODE()+"&ts="+String.valueOf(ts)+"&key="+shopService.getShopConfig().getKEY()));
        param.put("ts",ts);
        resultMap.put("param",param);
        //请求商城第三方
        String res = HttpUtil.get(shopService.getShopConfig().getURL()+ThirdPartyParam.TYPE_ATTR_URL.getName(), param);
        resultMap.put("resultText",res);
        Map<String,Object> resMap = JSONObject.parseObject(res, Map.class);
        if(resMap.get("returnCode").equals(1)){
            return  resultMap;
        }
        List<Map<String,Object>> listMap = (List<Map<String, Object>>) resMap.get("list");
        listMap.forEach(type->{
            ProductType productType = new ProductType();
            productType.setTypeName(String.valueOf(type.get("name")));
            //查询商品类型是否存在 存在更新 不存在新增
            List<ProductType> productTypes = productTypeService.queryAll(productType);
            productType.setCreateTime(new Date());
            productType.setPid(Long.valueOf(String.valueOf(type.get("id"))));
            if (CollectionUtils.isEmpty(productTypes)){
                productTypeService.add(productType,false);
            }else{
                productType.setId(productTypes.get(0).getId());
                productTypeService.modify(productType,false);
            }
            //添加规格
            List<Map<String,Object>> skuOptionList = (List<Map<String, Object>>) type.get("skuOptionList");
            skuOptionList.forEach(skuAttribute->{
                ProAttribute proAttribute = new ProAttribute();
                proAttribute.setIsValid(1);
                proAttribute.setProductAttributeName(String.valueOf(skuAttribute.get("skuOptionName")));
                //根据名字和是否启用查询是否存在
                List<ProAttribute> proAttributes = attributeService.queryAll(proAttribute);
                proAttribute.setIsValid(1);
                proAttribute.setCreateTime(new Date());
                if (CollectionUtils.isEmpty(proAttributes)){
                    attributeService.add(proAttribute,false);
                }else{
                    proAttribute.setId(proAttributes.get(0).getId());
                    attributeService.modify(proAttribute,false);
                }
                //添加规格和商品类型的关联关系
                ProductTypeAttribute productTypeAttribute = new ProductTypeAttribute();
                productTypeAttribute.setCreateTime(new Date());
                productTypeAttribute.setProAttributeId(proAttribute.getId());
                productTypeAttribute.setProductTypeId(productType.getId());
                List<ProductTypeAttribute> productTypeAttributes = productTypeAttributeService.queryAll(productTypeAttribute);
                if (CollectionUtils.isEmpty(productTypeAttributes)){
                    productTypeAttributeService.add(productTypeAttribute,false);
                }else{
                    productTypeAttribute.setId(productTypeAttributes.get(0).getId());
                    productTypeAttributeService.modify(productTypeAttribute,false);
                }
                //添加商品规格值
                List<Map<String,Object>> skuAttributeValues = (List<Map<String, Object>>) skuAttribute.get("skuOptionValueList");
                skuAttributeValues.forEach(skuAttributeValue->{
                    //查询是否存在
                    AttributeValue attributeValue = new AttributeValue();
                    attributeValue.setAttributeName(proAttribute.getProductAttributeName());
                    attributeValue.setAttributeValue(skuAttributeValue.get("skuOptionValueName").toString());
                    List<AttributeValue> attributeValues = attributeValueService.queryAll(attributeValue);
                    attributeValue.setAttributeNameId(proAttribute.getId());
                    if (CollectionUtils.isEmpty(attributeValues)){
                        attributeValueService.add(attributeValue,false);
                    }else{
                        attributeValue.setId(attributeValues.get(0).getId());
                        attributeValueService.modify(attributeValue,false);
                    }
                });
            });
        });
        return  resultMap;
    }




}
